/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-28 15:57:54
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-28 15:57:55
 * @FilePath: /sjf/lwp/block_queue/test.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "forward_blockqueue.hpp"
#include <cstdlib>
#include <time.h>

// 生产者
void *_producer(void *args)
{
    Anwtie::BlockQueue<int> *queue = (Anwtie::BlockQueue<int> *)(args);
    while (true)
    {
        sleep(1);
        int data = rand() % 10240;
        queue->push(data);
        std::cout << "producer->push_data:" << data << std::endl;
    }
    return nullptr;
}

// 消费者
void *_consumer(void *args)
{
    Anwtie::BlockQueue<int> *queue = (Anwtie::BlockQueue<int> *)(args);
    while (true)
    {
        sleep(1);
        int data;
        queue->pop(&data);
        std::cout << "consumer->pop_data:" << data << std::endl;
    }
    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    pthread_t producer, consumer;
    Anwtie::BlockQueue<int> *queue = new Anwtie::BlockQueue<int>;
    pthread_create(&producer, nullptr, _producer, (void *)queue);
    pthread_create(&consumer, nullptr, _consumer, (void *)queue);

    pthread_join(producer, nullptr);
    pthread_join(consumer, nullptr);

    delete queue;
    return 0;
}